package se581.banker.proxy;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import org.apache.log4j.Logger;

import se581.banker.event.TrackerEventWrapper;

/**
 * This is used to start general threads
 * 
 * @Not_Used
 * 
 * @author mmichalak
 *
 */
public class AsynchCommunication implements Runnable {
	static Logger logger = Logger.getLogger(AsynchCommunication.class.getName());
	
	Socket clientSocket = null;
	DataOutputStream outToServer = null;
	BufferedReader inFromServer = null;
	TrackerEventWrapper evWrapper = null;
	
	
	@Override
	public void run() {		
		logger.debug("Starting AsynchComm Thread...");

		String retFromServer;
		
		if (evWrapper == null || evWrapper.getEvent() == null) {
			logger.error("TrackerEventWrapper is null or the TrackerEvent is null:  " + evWrapper.toString());
			return;
		}
	  
		try {

			outToServer.writeBytes(evWrapper.getEvent().toEventLine() + '\n');
			
			retFromServer = inFromServer.readLine();
			logger.debug("FROM SERVER: " + retFromServer);
			evWrapper.setServerReturn(retFromServer);

			clientSocket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	



	public Socket getClientSocket() {
		return clientSocket;
	}


	public void setClientSocket(Socket clientSocket) {
		this.clientSocket = clientSocket;
	}


	public DataOutputStream getOutToServer() {
		return outToServer;
	}


	public void setOutToServer(DataOutputStream outToServer) {
		this.outToServer = outToServer;
	}


	public BufferedReader getInFromServer() {
		return inFromServer;
	}


	public void setInFromServer(BufferedReader inFromServer) {
		this.inFromServer = inFromServer;
	}


	public TrackerEventWrapper getEvWrapper() {
		return evWrapper;
	}



	public void setEvWrapper(TrackerEventWrapper evWrapper) {
		this.evWrapper = evWrapper;
	}

}
